package com.yonyou.pmclouds.productactive.controller;

import com.yonyou.pmclouds.basecom.util.EncryptUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
public class ProductActiveUtil {

    public static boolean checkTokenOfBuzi(String token, String secret, Map<String, Object> buzi) {
        List<Object> datas = (List<Object>) buzi.get("data");
        StringBuilder tokenString = new StringBuilder();
        for (Object data : datas) {
            Map<String, Object> order = (Map<String, Object>) data;
            String[] keys = order.keySet().toArray(new String[order.size()]);
            Arrays.sort(keys);
            for (String key : keys) {
                Object t = order.get(key);
                tokenString.append(key);
                tokenString.append("=");
                tokenString.append(t);
                tokenString.append("&");
            }
        }
        tokenString.append("key=");
        tokenString.append(secret);
        log.info("Activation token is {}, Activation info is {}", token, buzi);
        String expectToken = EncryptUtils.signWithMd5(tokenString.toString());
        return StringUtils.equals(expectToken, token);
    }
}
